package cn.sinata.wanglin.property.fragment;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.adapter.MyLeadMaterialAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.MyLeadMaterial;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 *物业---快递领物--已领取和未领取
 *
 */
public class ExpressDeliveryFragment extends RecyclerViewFragment {

    private MyLeadMaterialAdapter mAdapter;
    private List<MyLeadMaterial> mMyLeadMaterial = new ArrayList<>();
    private int type;
    private int pageNo = 0;
    private String userId;
    private String housingId;

    public static ExpressDeliveryFragment newInstance(int type){
        ExpressDeliveryFragment mExpressDeliveryFragment = new ExpressDeliveryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        mExpressDeliveryFragment.setArguments(bundle);
        return mExpressDeliveryFragment;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if (getArguments()!=null){
            type = getArguments().getInt("type");
        }
        mAdapter = new MyLeadMaterialAdapter(mMyLeadMaterial);
        return mAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {

        if (getArguments()!=null){
            type = getArguments().getInt("type");
        }
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        housingId = MyApplication.houseid;

        setItemDecoration(DEFAULT_MARGIN_DIVIDER);

        setEmptyViewClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setVisibility(View.GONE);
                showDialog();
                pullDownRefresh();
            }
        });

        showDialog();
        getData();
    }

    void getData(){
        HttpManager.getExpress(housingId,userId,type,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<MyLeadMaterial>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<MyLeadMaterial> myLeadMaterials) {
                        if (pageNo == 0) {
                            mMyLeadMaterial.clear();
                        }
                        if (myLeadMaterials.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("没有数据");
                            } else {
                                pageNo -- ;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mMyLeadMaterial.addAll(myLeadMaterials);
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
    @Override
    protected void pullDownRefresh() {
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        pageNo++;
        getData();
    }
}
